home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 1 / Mac Magazin and MacEasy Magazine CD - Issue 01.iso / Sharewarebibliothek / Powermac / C64 / SOURCE / Traps.c < prev    next >
Text File  |  1994-06-06  |  3KB  |  89 lines

  1. /*
  2.     Commodore 64 Emulator v0.4      Earle F. Philhower III 
  3.     Copyright (C) 1993-4            (st916w9r@dunx1.ocs.drexel.edu)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include "Processor.h"
  21. #include "Memory.h"
  22. #include "Error.h"
  23. #include "Traps.h"
  24.  
  25. #define MAXTRAPS 16
  26. static trap trapList[MAXTRAPS];
  27. static byte numTraps;
  28.  
  29. int TrapInitialize()
  30. {
  31.     numTraps=0;
  32.     return kNoError;
  33. }
  34.  
  35. int AddTrap(trap t)
  36. {
  37.     /* If we are at out limit for traps, then internal error */
  38.     if (numTraps==MAXTRAPS) InternalError(kTrapStorageOverflow);
  39.     
  40.     /* Check to see if the ROMs and the trap agree */
  41.     if (t.check[0]!=ByteAt(t.addr)) return kWrongROMError;
  42.     if (t.check[1]!=ByteAt(t.addr+1)) return kWrongROMError;
  43.     if (t.check[2]!=ByteAt(t.addr+2)) return kWrongROMError;
  44.     
  45.     /* Add this trap to out list */
  46.     trapList[numTraps++]=t;
  47.     
  48.     /* Either modify the low ROM or high ROM, as per address specified */
  49.     if (t.addr < 0xc000) loROM[t.addr&0x1fff]=0xff;
  50.     else hiROM[t.addr&0x1fff]=0xff;
  51.  
  52.     return kNoError;
  53. }
  54.  
  55. int DeleteTrap(word addr)
  56. {
  57.     int x, y;
  58.     
  59.     /* Search through traps added for the address to remove from */
  60.     for (x=0; x<numTraps; x++)
  61.         if (trapList[x].addr==addr) {
  62.         
  63.             /* Fix ROM to its initial state */
  64.             if (addr <= 0xc000) loROM[addr&0x1fff]=trapList[x].check[0];
  65.             else hiROM[addr&0x1fff]=trapList[x].check[0];
  66.             
  67.             /* Shift all of the traps down one slot */
  68.             for (y=x; y<numTraps-1; y++) trapList[y]=trapList[y+1];
  69.             
  70.             numTraps--;
  71.             return kNoError; }
  72.     
  73.     /* Attempted to remove a trap not added */
  74.     InternalError(kTrapNotInstalled);
  75. }
  76.  
  77. void TrapExecute()
  78. {
  79.     int x;
  80.     
  81.     /* Search for a trap at the current pc location, and if so execute it */
  82.     for (x=0; x<numTraps; x++)
  83.         if (trapList[x].addr==(pc-1)) {
  84.             (*trapList[x].call)();
  85.             return; }
  86.     /* This is not OUR trap code, so it's a nonimplemented instruction */
  87.     ini();
  88. }
  89.